﻿using System.Linq;
using Microsoft.Office.Core;
using Microsoft.Office.Interop.Outlook;
using Attachment = Microsoft.Office.Interop.Outlook.Attachment;
using Outlook.WorkItems.Extension.Addin.Actions;

namespace Outlook.WorkItems.Extension.Addin
{
    partial class BowieRibbonExtensibility
    {
        public bool SendAttachmentsReadMailButtonGetEnabled(IRibbonControl control)
        {
            MailItem item;
            if (!TryGetMailItemFromInspector(control.Context, out item))
                return false;

            return item.Attachments != null && item.Attachments.Count > 0;
        }

        public bool RelatedWorkItemReadMailButtonGetEnabled(IRibbonControl control)
        {
            var inspector = control.Context as Inspector;
            if (inspector == null)
                return false;

            var item = inspector.CurrentItem as MailItem;
            if (item == null)
                return false;

            return item.ItemProperties["RelatedWIIds"] != null && item.ItemProperties["RelatedWIIds"].Value != "" && item.ItemProperties["RelatedWIIds"].Value != null;
        }

        public bool TrackInWorkItemReadMailButtonGetPressed(IRibbonControl control)
        {
            MailItem item;
            if (!TryGetMailItemFromInspector(control.Context, out item))
                return false;

            return item.ItemProperties["RelatedWIIds"] != null && item.ItemProperties["RelatedWIIds"].Value != "" && item.ItemProperties["RelatedWIIds"].Value != null;
        }

        public void TrackInWorkItemReadMailButtonAction(IRibbonControl control, bool isPressed)
        {
            MailItem item;
            if (!TryGetMailItemFromInspector(control.Context, out item))
                return;

            if (isPressed)
            {
                var action = new TrackInWorkItemAction(new OutlookMailItem(item));
                action.Do();
            }
            else
            {
                var action = new UntrackWorkItemAction(new OutlookMailItem(item));
                action.Do();
            }
        }

        public void SendAttachmentsReadMailButtonAction(IRibbonControl control)
        {
            MailItem item;
            if (!TryGetMailItemFromInspector(control.Context, out item))
                return;


            if (item.Attachments == null || item.Attachments.Count <= 0)
                return;

            var action = new UploadAttachmentsToTeamPortalAction(item.Attachments.Cast<Attachment>());
            action.Do();
        }

        public void RelatedWorkItemReadMailButtonAction(IRibbonControl control)
        {
            MailItem item;
            if (!TryGetMailItemFromInspector(control.Context, out item))
                return;


            var action = new ViewRelatedWorkItemAction(item.ItemProperties);
            action.Do();
        }
        
        public void SendAttachmentContextualTabReadMailButtonAction(IRibbonControl control)
        {
            var inspector = control.Context as Inspector;
            if (inspector == null)
                return;

            var attchmentsSelection = inspector.AttachmentSelection;
            if (attchmentsSelection == null)
                return;

            var action = new UploadAttachmentsToTeamPortalAction(attchmentsSelection.Cast<Attachment>());
            action.Do();
        }

        public void SendAttachmentsReadMailMenuItemAction(IRibbonControl control)
        {
            var attchmentsSelection = control.Context as AttachmentSelection;
            if (attchmentsSelection == null)
                return;
            
            var action = new UploadAttachmentsToTeamPortalAction(attchmentsSelection.Cast<Attachment>());
            action.Do();
        }
    }
}
